package cn.tedu.ems.web;

import cn.tedu.ems.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 该类定义统一返回请求结果的方法,JSON对象类型(状态值+结果信息)
 *
 * @Author java.@Wqy
 * @Version 0.0.1
 */
@Data
public class JsonResult<T> implements Serializable {

    @ApiModelProperty("状态码")
    private Integer state;

    @ApiModelProperty("状态信息")
    private String message;

    @ApiModelProperty("返回数据")
    private T data;

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode,String message){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }
}
